/*
 * ===========================================================================================
 * = COPYRIGHT
 *          PAX Computer Technology (Shenzhen) Co., Ltd. PROPRIETARY INFORMATION
 *   This software is supplied under the terms of a license agreement or nondisclosure
 *   agreement with PAX Computer Technology (Shenzhen) Co., Ltd. and may not be copied or
 *   disclosed except in accordance with the terms in that agreement.
 *     Copyright (C) 2023 PAX Computer Technology (Shenzhen) Co., Ltd. All rights reserved.
 * ===========================================================================================
 */

import 'package:flutter/material.dart';
import 'package:pos_link/auto_data_source/auto_data_source_common.dart';
import 'package:pos_link/auto_data_source/auto_transaction_request_data_source.dart';
import 'package:pos_link/auto_data_source/auto_transaction_response_data_source.dart';
import 'package:pos_link/auto_pigeon/poslink_sdk_transaction.dart';
import 'package:pos_link/log/dart_log.dart';
import 'package:pos_link/model/response_db.dart';
import 'comm_setting_view_model.dart';

class TransactionViewModel extends ChangeNotifier {
  Future start(ResponseDataBase responseDataBase, List<Map> dataSource) async {
    POSLinkTransactionApi transactionApi = POSLinkTransactionApi();

    /// command
    Map cmdData = dataSource.first;
    switch (cmdData[kValue]) {
      case TransactionCommand.DoCreditRequest:
        TransactionDoCreditRequest req = TransactionReqData.formDoCreditRequest(dataSource);
        TransactionDoCreditResponse rsp = await transactionApi.doCredit(req);
        TransactionRspData.parseRspTransactionDoCreditResponse(rsp, responseDataBase.transactionData);
        break;
      case TransactionCommand.DoEbtRequest:
        TransactionDoEbtRequest req = TransactionReqData.formDoEbtRequest(dataSource);
        // Log.v('1------${req.transactionBehavior!.acceptedCardType}');
        // Log.v('2------${req.transactionBehavior!.programPromptsFlag}');
        // Log.v('3------${req.transactionBehavior!.entryMode}');
        // Log.v('4------${req.transactionBehavior!.transactionPromptBitmap}');
        TransactionDoEbtResponse rsp = await transactionApi.doEbt(req);
        TransactionRspData.parseRspTransactionDoEbtResponse(rsp, responseDataBase.transactionData);
        break;
      case TransactionCommand.DoLoyaltyRequest:
        TransactionDoLoyaltyRequest req = TransactionReqData.formDoLoyaltyRequest(dataSource);
        TransactionDoLoyaltyResponse rsp = await transactionApi.doLoyalty(req);
        TransactionRspData.parseRspTransactionDoLoyaltyResponse(rsp, responseDataBase.transactionData);
        break;
      case TransactionCommand.DoCheckRequest:
        TransactionDoCheckRequest req = TransactionReqData.formDoCheckRequest(dataSource);
        TransactionDoCheckResponse rsp = await transactionApi.doCheck(req);
        TransactionRspData.parseRspTransactionDoCheckResponse(rsp, responseDataBase.transactionData);
        break;
      case TransactionCommand.DoCashRequest:
        TransactionDoCashRequest req = TransactionReqData.formDoCashRequest(dataSource);
        TransactionDoCashResponse rsp = await transactionApi.doCash(req);
        TransactionRspData.parseRspTransactionDoCashResponse(rsp, responseDataBase.transactionData);
        break;
      case TransactionCommand.DoGiftRequest:
        TransactionDoGiftRequest req = TransactionReqData.formDoGiftRequest(dataSource);
        TransactionDoGiftResponse rsp = await transactionApi.doGift(req);
        TransactionRspData.parseRspTransactionDoGiftResponse(rsp, responseDataBase.transactionData);
        break;
      case TransactionCommand.DoDebitRequest:
        TransactionDoDebitRequest req = TransactionReqData.formDoDebitRequest(dataSource);
        TransactionDoDebitResponse rsp = await transactionApi.doDebit(req);
        TransactionRspData.parseRspTransactionDoDebitResponse(rsp, responseDataBase.transactionData);
        break;
      default:
        throw Exception('undefined function');
      //break;
    }
  }
}
